package com.ihr360.rest.webmvc;

import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Collections;

public class ControllerUtils {
    public static final Iterable<Resource<?>> EMPTY_RESOURCE_LIST = Collections.emptyList();
    /**
     * Wrap a resource as a {@link ResourceEntity} and attach given headers and status.
     *
     * @param status
     * @param headers
     * @param resource
     * @param <R>
     * @return
     */
    public static <R extends ResourceSupport> ResponseEntity<ResourceSupport> toResponseEntity(HttpStatus status,
                                                                                               HttpHeaders headers, R resource) {

        HttpHeaders hdrs = new HttpHeaders();

        if (headers != null) {
            hdrs.putAll(headers);
        }

        return new ResponseEntity<ResourceSupport>(resource, hdrs, status);
    }

    /**
     * Return an empty response that is only comprised of a status
     *
     * @param status
     * @return
     */
    public static ResponseEntity<ResourceSupport> toEmptyResponse(HttpStatus status) {
        return toEmptyResponse(status, null);
    }

    /**
     * Return an empty response that is only comprised of headers and a status
     *
     * @param status
     * @param headers
     * @return
     */
    public static ResponseEntity<ResourceSupport> toEmptyResponse(HttpStatus status, HttpHeaders headers) {
        return toResponseEntity(status, headers, null);
    }
}